package com.aiti.lulian.controller.crm;

import cn.hutool.core.util.StrUtil;
import com.aiti.base.core.base.controller.WebController;
import com.aiti.base.core.utils.message.Message;
import com.aiti.lulian.dto.EnterpriseDictDataDto;
import com.aiti.lulian.entity.EnterpriseDictData;
import com.aiti.lulian.service.IEnterpriseDictDataService;
import com.aiti.lulian.vo.EnterpriseDictDataVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "企业字典值控制层")
@RequestMapping("/enterprise/dict")
public class EnterpriseDictDataController extends WebController<EnterpriseDictData> {

    @Autowired
    private IEnterpriseDictDataService enterpriseDictDataService;

    @ApiOperation("企业新增自定义的字典值")
    @PostMapping("/add")
    public Message addEnterpriseDictData(@RequestBody EnterpriseDictDataDto dto) {
        String userId = dto.getUserId();
        if (StrUtil.isBlank(userId)) {
            userId = findUserId();
            if (StrUtil.isBlank(userId)) {
                return Fail("请先登录");
            }
            dto.setUserId(userId);
        }
        return enterpriseDictDataService.addEnterpriseDictData(dto);
    }

    @ApiOperation("企业修改字典值")
    @PutMapping("/update")
    public Message updateEnterpriseDictData(@RequestBody EnterpriseDictDataDto dto) {
        String userId = dto.getUserId();
        if (StrUtil.isBlank(userId)) {
            userId = findUserId();
            if (StrUtil.isBlank(userId)) {
                return Fail("请先登录");
            }
            dto.setUserId(userId);
        }
        return enterpriseDictDataService.updateEnterpriseDictData(dto);
    }

    @ApiOperation("企业删除字典值")
    @DeleteMapping("/delete")
    public Message deleteEnterpriseDictData(@RequestParam("baseId") String baseId,
                                            @RequestParam("sort") Integer sort,
                                            @RequestParam("enterpriseId") String enterpriseId,
                                            @RequestParam("dictTypeNo") String dictTypeNo,
                                            @RequestParam(value = "userId", required = false) String userId) {
        if (StrUtil.isBlank(userId)) {
            userId = findUserId();
            if (StrUtil.isBlank(userId)) {
                return Fail("请先登录");
            }
        }
        return enterpriseDictDataService.deleteEnterpriseDictData(baseId, sort, enterpriseId, dictTypeNo);
    }

    @ApiOperation("查询企业自定义的字典项列表")
    @GetMapping("/list")
    public Message getEnterpriseDictList(@RequestParam("enterpriseId") String enterpriseId,
                                         @RequestParam(value = "userId", required = false) String userId,
                                         @RequestParam("pageNum") Integer pageNum,
                                         @RequestParam("pageSize") Integer pageSize,
                                         @RequestParam(value = "module", required = false) String module) {
        if (StrUtil.isBlank(userId)) {
            userId = findUserId();
            if (StrUtil.isBlank(userId)) {
                return Fail("请先登录");
            }
        }
        Page<EnterpriseDictData> page = startPage(pageNum, pageSize);
        List<EnterpriseDictDataVo> enterpriseDictList = enterpriseDictDataService.getEnterpriseDictList(enterpriseId, module, page);
        return Success(getPageResult(enterpriseDictList, pageNum, pageSize, page.getTotal()));
    }

    @ApiOperation("查询企业自定义的字典值列表")
    @GetMapping("/data/list")
    public Message getEnterpriseDictDataList(@RequestParam("dictTypeNo") String dictTypeNo,
                                             @RequestParam("enterpriseId") String enterpriseId,
                                             @RequestParam(value = "userId", required = false) String userId) {
        if (StrUtil.isBlank(userId)) {
            userId = findUserId();
            if (StrUtil.isBlank(userId)) {
                return Fail("请先登录");
            }
        }
        return enterpriseDictDataService.getEnterpriseDictDataList(dictTypeNo, enterpriseId);
    }

    /**
     * 批量更新
     */
    @ApiOperation("排序")
    @PutMapping("/sort")
    public Message setEnterpriseDictDataSort(@RequestBody EnterpriseDictDataDto dto) {
        String userId = dto.getUserId();
        if (StrUtil.isBlank(userId)) {
            userId = findUserId();
            if (StrUtil.isBlank(userId)) {
                return Fail("请先登录");
            }
            dto.setUserId(userId);
        }
        return enterpriseDictDataService.setEnterpriseDictDataSort(dto);
    }
}
